﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cn.Edwon.Web.Support;
using System.Reflection;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Cellula.Model.Content;
using Cn.Cellula.Model.Security;

namespace Cn.Cellula.Website.Admin.Item {
    public partial class EditMultiItem : PageSupport {
        IItemBiz biz = BizFactory.Get<IItemBiz>();
        String KeyStr = null;
        protected void Page_Load(object sender, EventArgs e) {
            KeyStr = Request.QueryString["idl"];
            if (!Page.IsPostBack) {
                InitForm();
                LoadItem();
            }
        }
        private void InitForm() {
            IUserBiz ubiz = BizFactory.Get<IUserBiz>();
            IList<TSysRoleEO> list = ubiz.LoadAllRoles(this.Power.RoleLevel);
            this.ddl_EditableRole.DataSource = list;
            this.ddl_EditableRole.DataBind();
            this.ddl_EditableRole.Items.Insert(0, new ListItem("", ""));
            this.ddl_VisiableRole.DataSource = list;
            this.ddl_VisiableRole.DataBind();
            this.ddl_VisiableRole.Items.Insert(0, new ListItem("", ""));
        }
        private void LoadItem() {
            String keystr = "'" + KeyStr.Replace(",", "','") + "'";
            String hql = " from VItemListEO eo where eo.ItemKey in (" + keystr + ") ";
            hql += " and( eo.EditableRoleLevel is null or eo.EditableRoleLevel<=" + this.Power.RoleLevel + ") ";
            hql += " order by eo.ItemName ";
            IList<VItemListEO> list = biz.Find<VItemListEO>(hql);
            this.gvlist.DataSource = list;
            this.gvlist.DataBind();
        }
        protected void btn_save_Click(object sender, EventArgs e) {
            //---- 增加新项 ---------------------------------
            String keystr = "'" + KeyStr.Replace(",", "','") + "'";
            String hql = " from TItemEO eo where eo.ItemKey in (" + keystr + ") ";
            IList<TItemEO> list = biz.Find<TItemEO>(hql);
            int update = 0;
            foreach (TItemEO eo in list) {
                if (!String.IsNullOrEmpty(this.ddl_VisiableRole.SelectedValue)
                    && !String.IsNullOrEmpty(this.ddl_EditableRole.SelectedValue)) {
                    BizFactory.Get<IUserBiz>().BindPower(eo.ItemKey, this.ddl_VisiableRole.SelectedValue, this.ddl_EditableRole.SelectedValue,this.hid_Dept.Value, this.Actor.ID);
                    update++;
                }
            }
            BizFactory.Get<ILogBiz>().Info("批量更新"+update+"个对象的权限", this.Actor, Request);
            Response.Redirect("List.aspx?cid=" + Request.QueryString["cid"]);
        }
    }
}